using System.Collections.Specialized;

using RealDolmen.Helpers;

namespace RealDolmen.SharePoint.ListFeatures
{
    public abstract class ListFeatureObject
    {
        #region Abstract Methods
        public abstract string FullKey { get; }
        public abstract string Key { get; }
        #endregion // Abstract Methods

        #region Public Properties
        private ListFeatureDefinition featureDefinition;
        private ListFeature feature;
        private KeyManager uniqueKeyManager;
        private KeyManager identityKeyManager;

        internal KeyManager UniqueKeyManager
        {
            get { return uniqueKeyManager; }
        }

        internal KeyManager IdentityKeyManager
        {
            get { return identityKeyManager; }
        }

        public ListFeature Feature
        {
            set
            {
                feature = value;
                uniqueKeyManager = new SectionKeyManager(feature.KeyManager, FullKey);
            }
            protected internal get
            {
                return feature;
            }
        }

        public ListFeatureDefinition FeatureDefinition
        {
            get
            {
                return featureDefinition;
            }
            set
            {
                featureDefinition = value;
                identityKeyManager = new SectionKeyManager(featureDefinition.KeyManager, FullKey);
            }
        }
        #endregion // Public Properties


        #region Getters & Setters
        public virtual string GetDefaultElement(object name)
        {
            return FeatureDefinition.GetPropertyValue(IdentityKeyManager, name.ToString());
        }

        public void SetElement(object objName, string value)
        {
            string name = objName.ToString();

            if (feature != null)
            {
                string defaultValue = GetDefaultElement(name);

                if (value != defaultValue)
                    feature.SetPropertyValue(uniqueKeyManager, name, value);
                else
                    feature.DeletePropertyValue(uniqueKeyManager, name);
            }
        }
        public string GetElement(object objName) 
        {
            string name = objName.ToString();
            
            if (feature != null)
            {
                string value = feature.GetPropertyValue(uniqueKeyManager, name);

                if (value != null)
                    return value;
            }
            return GetDefaultElement(name); 
        }

        public void SetAttribute() { }
        public string GetAttribute() { return string.Empty; }

        public string GetFromCollection(object obj,  NameValueCollection collection, RetrievalMethod retrievalMethod)
        {
            string key = ConvertKey(obj, retrievalMethod);
            return collection[key];
        }

        public string ConvertKey(object obj, RetrievalMethod retrievalMethod)
        {
            KeyManager keyManager = null;

            if (retrievalMethod == RetrievalMethod.Unique)
                keyManager = UniqueKeyManager;
            else if (retrievalMethod == RetrievalMethod.Identity)
                keyManager = IdentityKeyManager;

           return keyManager.GenerateKey(obj.ToString());
        }

        #endregion // Getters & Setters
    }
    public enum RetrievalMethod
    {
        Unique,
        Identity
    }
}

